package com.ruoyi.quartz.task;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jhczb.domain.JhcZb;
import com.ruoyi.jhczb.mapper.JhcZbMapper;
import com.ruoyi.scrb.domain.ScrbCl;
import com.ruoyi.scrb.mapper.ScrbClMapper;
import com.ruoyi.scrball.domain.ScrbAll;
import com.ruoyi.scrball.mapper.ScrbAllMapper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    ScrbAllMapper scrbAllMapper;
    @Autowired
    JhcZbMapper jhcZbMapper;
    @Autowired
    ScrbClMapper scrbClMapper;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 每天总表插入数据
     */
    public void insertAllData() {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        ScrbAll scrbAll = new ScrbAll();
        scrbAll.setcDate(date);
        if (null == scrbAllMapper.selectScrbAllByCDate(date))
            scrbAllMapper.insertScrbAll(scrbAll);

        JhcZb jhcZb = new JhcZb();
        jhcZb.setcDate(date);
        if (null == jhcZbMapper.selectJhcZbByCDate(date))
            jhcZbMapper.insertJhcZb(jhcZb);
    }


    /**
     * 每天早上取数（从cl到总表）
     */
    public void insertYield() {
        File file = new File("\\\\192.168.1.89\\bbxt\\scrb\\CL.xls");// 将读取的txt文件
        File file2 = new File("\\\\192.168.1.89\\bbxt\\scrb\\CLGreen.xls");// 将生成的excel表格

        if (file.exists() && file.isFile()) {

            InputStreamReader read = null;
            String line = "";
            BufferedReader input = null;
            WritableWorkbook wbook = null;
            WritableSheet sheet;

            try {
                read = new InputStreamReader(new FileInputStream(file), "gbk");
                input = new BufferedReader(read);
                wbook = Workbook.createWorkbook(file2);// 根据路径生成excel文件
                sheet = wbook.createSheet("CL", 0);// 新标签页
                int m = 0;// excel行数
                int n = 0;// excel列数
                Label t;
                while ((line = input.readLine()) != null) {
                    String[] words = line.split("\t");// 把读出来的这行根据空格或tab分割开
                    for (int i = 0; i < words.length; i++) {
                        t = new Label(n, m, words[i].trim());
                        sheet.addCell(t);
                        n++;
                    }
                    n = 0;// 回到列头部
                    m++;// 向下移动一行
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (RowsExceededException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            } finally {
                try {
                    wbook.write();
                    wbook.close();
                    input.close();
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
        } else {
        }

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        String date = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        String fileUrl = "\\\\192.168.1.89\\bbxt\\scrb\\CLGreen.xls";
        File templateFile = new File(fileUrl);
        InputStream is = null;
        HSSFWorkbook workbook = null;
        HSSFSheet sheet = null;
        try {
            is = new FileInputStream(templateFile);// 将excel文件转为输入流
            workbook = new HSSFWorkbook(is);// 创建个workbook，
            // 获取第一个sheet
            sheet = workbook.getSheetAt(0);
            ScrbCl scrbCl = new ScrbCl();
            scrbCl.setcDate(date);

            //全焦
            scrbCl.setcLjgxQjDrSj(sheet.getRow(8).getCell(5).toString());
            scrbCl.setcLjgxQjLjSj(sheet.getRow(8).getCell(7).toString());
            //冶金焦
            scrbCl.setcLjgxYjjDrSj(sheet.getRow(9).getCell(5).toString());
            scrbCl.setcLjgxYjjLjSj(sheet.getRow(9).getCell(7).toString());
            //煤焦油
            scrbCl.setcLjgxMjyDrSj(sheet.getRow(10).getCell(5).toString());
            scrbCl.setcLjgxMjyLjSj(sheet.getRow(10).getCell(7).toString());
            //硫酸铵
            scrbCl.setcLjgxLsaDrSj(sheet.getRow(11).getCell(5).toString());
            scrbCl.setcLjgxLsaLjSj(sheet.getRow(11).getCell(7).toString());
            //轻苯
            scrbCl.setcLjgxQbDrSj(sheet.getRow(12).getCell(5).toString());
            scrbCl.setcLjgxQbLjSj(sheet.getRow(12).getCell(7).toString());
            //1#回转窑
            scrbCl.setcQyjgxHzy1DrSj(sheet.getRow(42).getCell(5).toString());
            scrbCl.setcQyjgxHzy1LjSj(sheet.getRow(42).getCell(7).toString());
            //2#回转窑
            scrbCl.setcQyjgxHzy2DrSj(sheet.getRow(19).getCell(5).toString());
            scrbCl.setcQyjgxHzy2LjSj(sheet.getRow(19).getCell(7).toString());
            //宝龙球团
            scrbCl.setcQyjgxBlqtDrSj(sheet.getRow(43).getCell(5).toString());
            scrbCl.setcQyjgxBlqtLjSj(sheet.getRow(43).getCell(7).toString());

            ////三棒
            //scrbCl.setcZcgxEbDrSj(sheet.getRow(35).getCell(5).toString());
            //scrbCl.setcZcgxEbLjSj(sheet.getRow(35).getCell(7).toString());
            ////中型
            //scrbCl.setcZcgxZxDrSj(sheet.getRow(30).getCell(5).toString());
            //scrbCl.setcZcgxZxLjSj(sheet.getRow(30).getCell(7).toString());
            ////钢绞线
            //scrbCl.setcZcgxJszpGjxDrSj(sheet.getRow(40).getCell(5).toString());
            //scrbCl.setcZcgxJszpGjxLjSj(sheet.getRow(40).getCell(7).toString());
            ////镀锌线
            //scrbCl.setcZcgxJszpDxxDrSj(sheet.getRow(41).getCell(5).toString());
            //scrbCl.setcZcgxJszpDxxLjSj(sheet.getRow(41).getCell(7).toString());
            ////金属材料一连铸
            //scrbCl.setcDlgxLzj1DrSj(sheet.getRow(22).getCell(5).toString());
            //scrbCl.setcDlgxLzj1LjSj(sheet.getRow(22).getCell(7).toString());
            ////金属材料二连铸
            //scrbCl.setcDlgxLzj2DrSj(sheet.getRow(23).getCell(5).toString());
            //scrbCl.setcDlgxLzj2LjSj(sheet.getRow(23).getCell(7).toString());
            ////金属材料三连铸
            //scrbCl.setcDlgxLzj3DrSj(sheet.getRow(24).getCell(5).toString());
            //scrbCl.setcDlgxLzj3LjSj(sheet.getRow(24).getCell(7).toString());


            if (null == scrbClMapper.selectScrbClByCDate(date)) {
                //插入数据
                scrbClMapper.insertScrbCl(scrbCl);
            } else {
                //更新数据
                scrbClMapper.updateScrbCl(scrbCl);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 测试方法
     */
    public void test() {

    }
}
